package com.lqk.demo.pessimisticlock.reentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;



/**
 * @author lqk
 * @date 2021/12/22 20:44
 */
@Slf4j
public class ReentrantLock2 {
    /**
     * ReentrantLock:
     *  可打断特性
     */
    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                /**
                 * 如果没有竞争那么此方法就会获取 lock 对象锁
                 * 如果有竞争就进入阻塞队列，可以被其他线程用 interrupt 方法打断
                 */
                log.debug("尝试获得锁");
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("没有获得锁");
                return;
            }

            try{
                log.debug("获取到锁");
            }finally {
                lock.unlock();
            }
        }, "t1");

        lock.lock();
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug("打断t1");
        t1.interrupt();
    }
}
